package com.examplee.puzle;



import android.app.Activity;
import android.net.Uri;
import android.os.Bundle;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View.OnClickListener;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;


@SuppressWarnings("deprecation")
public class SelImagenActivity extends Activity implements OnClickListener{
	ImageView visor,img01,img02;
	ImageView img03;
	Integer[] imageIDs = {
			 R.drawable.familiapig_final,
			 R.drawable.familiapig2,
			 R.drawable.familiapig3,
			 R.drawable.peppapig2,
			 R.drawable.peppapig3
			 };
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_selimagen);
       /* visor=(ImageView)findViewById(R.id.imgvisor);
        img01=(ImageView)findViewById(R.id.imgpicture1);
        img02=(ImageView)findViewById(R.id.imgpicture2);
        img01.setOnClickListener(this);
        img02.setOnClickListener(this);*/
        Gallery gallery = (Gallery) findViewById(R.id.gallery1);
        gallery.setAdapter(new ImageAdapter(this));
		img03 = (ImageView) findViewById(R.id.image1);
		registerForContextMenu(img03);
		gallery.setOnItemClickListener(new OnItemClickListener() {
			public void onItemClick(AdapterView<?> parent, View v, int position,long id)
	{
				Toast.makeText(getBaseContext(),"pic" + (position + 1) + " selected",
						Toast.LENGTH_SHORT).show();
				// display the images selected
				ImageView imageView = (ImageView) findViewById(R.id.image1);
				imageView.setImageResource(imageIDs[position]);
				imageView.setTag(getBaseContext().getResources().getResourceEntryName(imageIDs[position]));
				//imageView.setTag(getBaseContext().getResources().getDrawable(imageIDs[position]));
		}
	});
	}
	
	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		/*switch (v.getId()) {
		case R.id.imgpicture1:
			visor.setImageResource(R.drawable.peppapig);
			break;
		case R.id.imgpicture2:
			visor.setImageResource(R.drawable.georgepig);
			break;
		default:
			break;
		}*/

	}
	
	public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
   		super.onCreateContextMenu(menu, v, menuInfo);
   		if(v.getId()==R.id.image1)
   			getMenuInflater().inflate(R.menu.main, menu);
   	
   	}
	 @Override
		public boolean onContextItemSelected(MenuItem item) {
	    	
	    	switch (item.getItemId()) {
			case R.id.opcion1:
				Toast.makeText(getApplicationContext(), "Has pulsado la opcion 1", Toast.LENGTH_SHORT).show();
				
				String imgtag=img03.getTag().toString();
				String cad = imgtag;
				Intent data = new Intent();
                data.setData(Uri.parse(cad));
                setResult(RESULT_OK, data);
                finish();
				return true;
			/*case R.id.opcion2:
				Toast.makeText(getApplicationContext(), "Has pulsado la opcion 2", Toast.LENGTH_SHORT).show();
				return true;*/
			default:
				return super.onContextItemSelected(item);
			}
	    	
		}
	
	public class ImageAdapter extends BaseAdapter {
		private Context context;
		private int itemBackground;
		public ImageAdapter(Context c)
		{
			context = c;
			// sets a grey background; wraps around the images
			TypedArray a =obtainStyledAttributes(R.styleable.MyGallery);
			itemBackground = a.getResourceId(R.styleable.MyGallery_android_galleryItemBackground, 0);
			a.recycle();
		}
		// returns the number of images
		public int getCount() {
			return imageIDs.length;
		}
		// returns the ID of an item
		public Object getItem(int position) {
			return position;
		}
		// returns the ID of an item
		public long getItemId(int position) {
			return position;
		}
		// returns an ImageView view
		public View getView(int position, View convertView, ViewGroup parent) {
			ImageView imageView = new ImageView(context);
			imageView.setImageResource(imageIDs[position]);
			imageView.setLayoutParams(new Gallery.LayoutParams(100, 100));
			imageView.setBackgroundResource(itemBackground);
			return imageView;
		}
	}
	
}
